package com.hyyt.shopjsp.home.controller;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.hyyt.shopjsp.home.pojo.ShopHomeMiddleCategory;
import com.hyyt.shopjsp.home.service.IShopHomeMiddleCategoryService;
import com.hyyt.shopjsp.util.tree.NodeUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.basic.service.IBasicActorPurviewService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.home.pojo.ShopHomeMiddleCategoryBilateral;
import com.hyyt.shopjsp.home.service.IShopHomeMiddleCategoryBilateralService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/03/28 0019下午 2:41.
 */
@Controller
@RequestMapping("/back/shopHomeMiddleCategoryBilateral")
public class ShopHomeMiddleCategoryBilateralController extends BaseController {
    private Logger logger = Logger.getLogger(ShopHomeMiddleCategoryBilateralController.class);
    private String basePath = "/back/shop/shopHomeMiddleCategoryBilateralAndTab/";
    @Resource
    private IShopHomeMiddleCategoryBilateralService homeMiddleCategoryBilateralService;
	@Resource
	private IShopHomeMiddleCategoryService shopHomeMiddleCategoryService;//首页中间分类service

    /**
	 * 进入首页中间分类两侧维护
	 * @param param
	 * @return
	 */
	@RequestMapping("gotoShopHomeMiddleCategoryBilateralPage")
	public String gotoShopHomeMiddleCategoryBilateralPage(Map<String, Object> param){
		param.put("categoryId", "1");//首页中间分类id
		return basePath+"bilateralAndTabList";
	}

	/**
	 * 查询所有首页中间分类两侧维护
	 * @param categoryId   分类id
	 * @param page
	 * @param response
	 * @param title		主题
	 * @param isShow	是否显示
	 * @param type		类型
	 */
	@RequestMapping(value="listShopHomeMiddleCategoryBilateral",method=RequestMethod.POST)
	@ResponseBody
	public void listShopHomeMiddleCategoryBilateral(String categoryId,String title,String isShow,String type,Page page, HttpServletResponse response){
		Map<String, Object> param = new HashMap<>();
		param.put("categoryId", categoryId);
		if(Utils.objectIsNotEmpty(title)){//是否检索名称
			param.put("title", title.trim());
		}
		if(Utils.objectIsNotEmpty(isShow)){//是否检索类型名称
			param.put("isShow", isShow);
		}
		if(Utils.objectIsNotEmpty(type)){//是否检索类型名称
			param.put("type", type);
		}
		param.put("sortColumns", " sortCode asc");
		int totalRecordCount =homeMiddleCategoryBilateralService.countShopHomeMiddleCategoryBilateral(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopHomeMiddleCategoryBilateral> list =homeMiddleCategoryBilateralService.queryShopHomeMiddleCategoryBilateral(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", list);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
     * 根据首页中间分类两侧维护id获得对象
     *
     * @param bilateralId
     */
    @RequestMapping(value = "getShopHomeMiddleCategoryBilateralObj", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getShopHomeMiddleCategoryBilateralObj(Integer bilateralId) {
		if (Utils.objectIsNotEmpty(bilateralId)) {
			ShopHomeMiddleCategoryBilateral categoryBilateral = homeMiddleCategoryBilateralService.loadShopHomeMiddleCategoryBilateral(bilateralId);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(categoryBilateral));
		}
		return ResultMsg.create(false);
	}

    /**
     * 保存或者修改首页中间分类两侧维护
     *
     * @param bilateral
     * @return
     */
    @RequestMapping(value = "saveOrUpdateShopHomeMiddleCategoryBilateral", method = RequestMethod.POST)
    @ResponseBody
	public ResultMsg saveOrUpdateShopHomeMiddleCategoryBilateral(ShopHomeMiddleCategoryBilateral bilateral) {
		if (Utils.objectIsNotEmpty(bilateral)) {
			BasicUsers basicUsers = getSessionUsers();
			if (Utils.objectIsEmpty(bilateral.getBilateralId())) {
				Timestamp createTime = new Timestamp(System.currentTimeMillis()); 
				bilateral.setCreateTime(createTime);
				bilateral.setPublishUser(basicUsers.getUserName());
				homeMiddleCategoryBilateralService.saveShopHomeMiddleCategoryBilateral(bilateral);
			} else {
				Timestamp updateTime = new Timestamp(System.currentTimeMillis()); 
				bilateral.setUpdateTime(updateTime);
				bilateral.setModifyUser(basicUsers.getUserName());
				homeMiddleCategoryBilateralService.updateShopHomeMiddleCategoryBilateral(bilateral);
			}
		}
		return ResultMsg.create(true);
	}

    /**
     * 根据传入的首页中间分类两侧维护id删除
     *
     * @param bilateralId
     */
    @RequestMapping(value = "deleteHomeMiddleCategoryBilateral", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteHomeMiddleCategoryBilateral(String bilateralId) {
		if (Utils.objectIsNotEmpty(bilateralId)) {
			homeMiddleCategoryBilateralService.deleteShopHomeMiddleCategoryBilateral(Integer.parseInt(bilateralId));
		}
		return ResultMsg.create(true);
	}

	/**
	 * 根据id展示左侧首页楼层树形结构
	 * @param response
	 */
	@RequestMapping("getNodes")
	public void getNodes(HttpServletResponse response){
		Map<String, Object> param = new HashMap<>();
		String [] selectColmunsArray={"categoryId","categoryName","parentId","isLeaf"};
		param.put("selectColmunsArray",selectColmunsArray);
		param.put("sortColumns", "sortCode asc");
		List<ShopHomeMiddleCategory> categoryList=shopHomeMiddleCategoryService.queryShopHomeMiddleCategoryAll(param);
		List nodeList = new ArrayList();
		HashMap nodeMap = new HashMap();
		for(int i = 0; i < categoryList.size(); i++) {
			ShopHomeMiddleCategory shmc = categoryList.get(i);
			nodeMap = new HashMap();
			nodeMap.put("id",shmc.getCategoryId());
			nodeMap.put("text", shmc.getCategoryName());
			nodeMap.put("isLeaf", shmc.getIsLeaf());
			String parentId = String.valueOf(shmc.getParentId());
			if (Utils.objectIsEmpty(parentId)) {
				parentId = "0";
			}
			nodeMap.put("parentId",parentId);
			nodeList.add(nodeMap);
		}
		//获取分类的json字符串信息
		String jsonInfo= NodeUtil.getNodeJson(nodeList,"首页楼层分类");
		ResultMsg.create(jsonInfo, response, false);
	}

}
